#pragma once
#include "abstractdatamanager.h"

class CPasswordProfile
{
public:
	CPasswordProfile()
	{
		m_nID = 0;
		m_nClassID = 0;
		m_strTitle = L"";
		m_strUserName = L"";
		m_strPassword = L"";
		m_strAddress = L"";
		m_strRemark = L"";
		m_tCreate = CTime::GetCurrentTime();
		m_tEdit = CTime::GetCurrentTime();
		m_tAccess = CTime::GetCurrentTime();
		m_tExpire = CTime::GetCurrentTime();
		m_strAttachName = L"";
		m_nIcon	= 0;
		m_pAttachContent = NULL;
		m_bAdministrator = FALSE;
		m_nACLen = 0;
		m_nMasterID = 0;
		m_pPassword = NULL;
		m_pUserName = NULL;
		
	}
	~CPasswordProfile()
	{
		if(m_pAttachContent)
		{
			delete [] m_pAttachContent;
			m_pAttachContent = NULL;
		}
		if(m_pUserName)
		{
			delete[] m_pUserName;
			m_pUserName = NULL;
		}
		if(m_pPassword)
		{
			delete[] m_pPassword;
			m_pPassword = NULL;
		}
	}
	
public:
	int				m_nID;
	int				m_nClassID;
	int				m_nMasterID;
	CString			m_strTitle;
	CString			m_strUserName;
	BYTE*			m_pUserName;
	int				m_nUNLen;
	CString			m_strPassword;
	BYTE*			m_pPassword;
	int				m_nPswLen;
	CString			m_strAddress;
	CString			m_strRemark;
	CTime			m_tCreate;
	CTime			m_tEdit;
	CTime			m_tAccess;
	CTime			m_tExpire;
	CString			m_strAttachName;
	int				m_nIcon;
	BYTE*			m_pAttachContent;
	BOOL			m_bAdministrator;
	int				m_nACLen;
};
class CPwMaster
{
public:
	CPwMaster()
	{
		m_nID = 0;
		m_Type = L"PSW";
		m_Name = L"";
		memset(m_MasterSeed,0,16);
		memset(m_EncryptionIV,0,16);
		memset(m_ContentsHash,0,32);
		memset(m_MasterSeed2,0,32);
		m_KeyEncRounds = 0;
		m_Update = FALSE;
	}
public:
	int			m_nID;
	CString		m_Type;
	CString		m_Name;
	int			m_PswAndKey;
	BYTE		m_MasterSeed[16]; ///< Seed that gets hashed with the user key to form the final key.
	UINT8		m_EncryptionIV[16]; ///< IV used for content encryption.
	BYTE		m_ContentsHash[32]; ///< SHA-256 hash of the database, used for integrity checking.

	BYTE		m_MasterSeed2[32]; ///< Seed used for the dwKeyEncRounds AES transformations.
	int			m_KeyEncRounds; ///< Number of master key transformations.
	CString		m_KeyFile;
	BOOL		m_Update;
};
typedef CAtlList<CPwMaster*> PwMasterList;
typedef CAtlList<CPasswordProfile*> PswPfList;

class CPasswordManager :
	public CAbstractDataManager
{
public:
	CPasswordManager(void);
	virtual ~CPasswordManager(void);
	BOOL Add(const CPasswordProfile& pswPf);
	int Delete(const int nID);
	int Update(const CPasswordProfile& pswPf);
	PswPfList* Get(const int nMasterID);
	int MoveTo(const int iClassID,const int iID);
	int SetAccess(const int nID);
	BOOL Add(const CPwMaster& pswPf);
	int DeleteMaster(const int nID);
	BOOL Update(const CPwMaster& pswPf);
	int GetNextID();
	CPwMaster* Get(const CString strName);
	BOOL IsExist(const CString strName);
	PwMasterList* GetList(const CString strType);
	void BeginSave();
	BOOL Save(const CPwMaster& pswPf);
	void EndSave(BOOL bCommit);
protected:
	CComPtr<ISQLiteComStatement3> pSQLStatement;
};
